package com.study.bigwork.teacher.dao;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.study.bigwork.entity.Teacher;
import com.study.bigwork.entity.User;
import com.study.bigwork.util.GetInfo;

import io.rong.models.response.TokenResult;

@Repository
public class TeacherDao {
	@Resource
	private SessionFactory sessionFactory;
	
	//添加教师
	public boolean addTeacher(Teacher teacher) {
		Session session=sessionFactory.getCurrentSession();
		Serializable result = session.save(teacher);
		int count = (int) result;
		return count>0 ? true : false;
	}
	
	//获取教师信息
	//使用手机号和密码登录
	public Teacher findTeacherByPhonenumberAndPassword(String pNumber,String password){
		Teacher teacher=null;
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery("from Teacher where pNumber=? and password=?");
		query.setParameter(0, pNumber);
		query.setParameter(1, password);
		teacher=(Teacher) query.uniqueResult();
		if(teacher!=null) {
			return teacher;
		}
		return null;
	}
	//使用手机号和验证码登录
	public Teacher findTeacherByPhonenumber(String pNumber){
		Teacher teacher=null;
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery("from Teacher where pNumber=?");
		query.setParameter(0, pNumber);
		teacher=(Teacher) query.uniqueResult();
		if(teacher!=null) {
			return teacher;
		}else {
			//未注册
			Teacher teacher3=new Teacher();
			GetInfo getInfo=new GetInfo();
			TokenResult tokenResult = getInfo.getToken("teacher");
			String chat_id = tokenResult.getUserId();
			String chat_token = tokenResult.getToken();
			teacher3.setChat_id(chat_id);
			teacher3.setChat_token(chat_token);
			teacher3.setNickname(pNumber);
			teacher3.setpNumber(pNumber);
			session.save(teacher3);
			return teacher3;
		}
		
	}
	//获取聊天的老师
	public Teacher getChatTeacher(String chat_id){
		Teacher teacher = new Teacher();
		Session session=sessionFactory.getCurrentSession();
		String hql = "from Teacher where chat_id=?"; 
		Query query = session.createQuery(hql);
		query.setParameter(0, chat_id);
		return teacher;
	}
	
	//判断教师是否存在
	public boolean isExistTeacher(String pNumber) {
		Teacher teacher = new Teacher();
		Session session=sessionFactory.getCurrentSession();
		String hql = "from Teacher where pNumber=?"; 
		Query query = session.createQuery(hql);
		query.setParameter(0, pNumber);
		teacher=(Teacher) query.uniqueResult();
		if(teacher!=null) {
			return true;
		}
		return false;
	}
	//获取chat信息
	public Teacher getChatInfo(Teacher teacher) {
		Session session=this.sessionFactory.getCurrentSession();
		Query query =session.createQuery("from Teacher where chat_id=?");
		query.setParameter(0, teacher.getChat_id());
		return (Teacher) query.uniqueResult();
	}	
}
